import globalVisionEntertainment.nba.Database;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.sql.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class DetailFrame extends SheetFrame implements WindowListener
{

	// private JButton showButton;
	// private JSpinner averageRangeSpinner;
	private final int recentRange;
	private final String[] playerNames;

	public DetailFrame(String[] playerNames, int recentRange, int width)
	{
		super(false);
		
		this.playerNames = playerNames;
		this.recentRange = recentRange;

		setSize(width, 400);

		// contextMenu = new JPopupMenu();
		// addPopup(table, contextMenu);

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());

		panel.add(new JScrollPane(table), BorderLayout.CENTER);

		// JPanel panel2 = new JPanel();
		// panel2.setLayout(new FlowLayout());

		// showButton = new JButton("Show");
		// showButton.addActionListener(this);
		// panel2.add(showButton);
		// panel2.add(new JLabel("average for last"));
		// averageRangeSpinner = new JSpinner(new SpinnerNumberModel());
		//
		// averageRangeSpinner.setPreferredSize(new Dimension(50, 30));
		// averageRangeSpinner.setValue(10);
		// panel2.add(averageRangeSpinner);
		// panel2.add(new JLabel("games"));

		// panel.add(panel2, BorderLayout.SOUTH);

		getContentPane().add(panel);

		this.addWindowListener(this);

		// getContentPane().add(table);
	}
	

	@Override
	protected void loadData() throws IOException
	{

		Connection conn = null;
		try
		{
			conn = Database.getConnection();

			for (String playerName : playerNames)
			{
				// @formatter:off
				String sql = "SELECT * FROM nba.`player stat total` \n" + "where `Player Name`=? \n"
						+ "order by `Date` desc \n" + "limit 0, ?";
				// @formatter:on

				PreparedStatement statement = conn.prepareStatement(sql);
				statement.setString(1, playerName);
				statement.setInt(2, recentRange);

				statement.execute();

				ResultSet resultSet = statement.getResultSet();
				ResultSetMetaData metaData = resultSet.getMetaData();

				int columnCount = metaData.getColumnCount();
				if (tableModel.getColumnCount() == 0)
				{
					for (int i = 1; i <= columnCount; i++)
						tableModel.addColumn(metaData.getColumnLabel(i));
				}

				resultSet.first();

				do
				{
					Object[] rowData = new Object[columnCount + 1];
					for (int i = 0; i < columnCount; i++)
					{
						rowData[i] = resultSet.getString(i + 1);
					}
					tableModel.addRow(rowData);

				} while (resultSet.next());

			}

		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (conn != null)
					conn.close();
			}
			catch (SQLException e)
			{
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	private static void addPopup(Component component, final JPopupMenu popup)
	{
		component.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mousePressed(MouseEvent e)
			{
				if (e.isPopupTrigger())
					showMenu(e);
			}

			@Override
			public void mouseReleased(MouseEvent e)
			{
				if (e.isPopupTrigger())
					showMenu(e);
			}

			private void showMenu(MouseEvent e)
			{
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	@Override
	public void windowOpened(WindowEvent e)
	{
		super.windowOpened(e);
		
		int characterLength = 70;
		String title = Stringhelper.innerConcatenate(playerNames, ", ");

		if (title.length() > characterLength)
		{
			title = title.substring(0, characterLength);
			title += "...";
		}

		title = "Recent " + recentRange + " games for " + title;

		setTitle(title);
	}

	// @formatter:off
	@Override
	public void windowClosing(WindowEvent e)
	{
	}

	@Override
	public void windowClosed(WindowEvent e)
	{
	}

	@Override
	public void windowIconified(WindowEvent e)
	{
	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
	}

	@Override
	public void windowActivated(WindowEvent e)
	{
	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
	}

	// @formatter:on

	// @Override
	// public void actionPerformed(ActionEvent e)
	// {
	// assert e.getSource() == showButton;
	//
	// ArrayList<String> list = new ArrayList<String>();
	// for (int i = 0; i < tableModel.getRowCount(); i++)
	// {
	// if (tableModel.getValueAt(i, 0).equals(true))
	// {
	// list.add((String) tableModel.getValueAt(i, 1));
	// System.out.println(tableModel.getValueAt(i, 1));
	// }
	// }
	//
	// String[] playerNames = new String[list.size()];
	// list.toArray(playerNames);
	//
	// AverageFrame f = new AverageFrame(playerNames, (int) averageRangeSpinner.getValue());
	// f.setVisible(true);
	//
	// }

}
